#include "Stack.h"


void StackInit(Stack* ps)
{	
	assert(ps);

	ps->_data = (STDataType*)malloc(sizeof(STDataType)*4);
	if (ps->_data == NULL)
	{
		perror("mallloc fail:");
		exit(-1);
	}
	ps->_top = 0;
	ps->_capacity = 4;
	
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		STDataType* tmp = (STDataType*)realloc(ps->_data,sizeof(STDataType) * ps->_capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail:");
			exit(-1);
		}
		ps->_capacity *= 2;
		ps->_data= tmp;
	}
	ps->_data[ps->_top] = data;
	ps->_top++;

}

void StackPop(Stack* ps)
{
	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_data[ps->_top - 1];
}

int StackSize(Stack* ps)
{
	return ps->_top;
}

int StackEmpty(Stack* ps)
{
	return ps->_top == 0;
}

void StackDestroy(Stack* ps)
{
	free(ps->_data);
	ps->_top = 0;
	ps->_capacity = 0;
}
